package com.yishun.springboot.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yishun.springboot.service.TbPageService;

@RestController
@RequestMapping("/TbPage")
public class TbPageController {

	@Autowired
	TbPageService pageService;

	/**
	 * 查询已拥有权限跟平台所有权限
	 * 
	 * @param jsonObject
	 * @return
	 */
	@RequestMapping("/getRoleAndPage")
	public String getRoleAndPage(@RequestBody JSONObject jsonObject) {
		return pageService.getRoleAndPage(jsonObject).toJSONString();
	}

	/**
	 * 新增页面跟按钮权限
	 * 
	 * @param jsonObject
	 * @return
	 */
	@RequestMapping("/addMultiPage")
	public String addMultiPage(@RequestBody JSONObject jsonObject) {
		JSONArray pageList = jsonObject.getJSONArray("pageList");
		JSONArray menuList = jsonObject.getJSONArray("menuList");
		String roleIdOrUsername = jsonObject.getString("roleIdOrUsername");
		List<Map<String, Object>> list = new ArrayList<>();
		for (int i = 0; i < pageList.size(); i++) {
			JSONObject jsonObjects = pageList.getJSONObject(i);
			Map<String, Object> map = new HashMap<>();

			// 将JSONObject中的键值对转换为Map中的键值对
			map.put("roleIdOrUsername", roleIdOrUsername);
			map.put("pageId", jsonObjects.getString("pageId")); // 需要修改id
																// 看mapper字段名
																// 更正过来
			list.add(map);
		}

		List<Map<String, Object>> menuLists = new ArrayList<>();
		for (int i = 0; i < menuList.size(); i++) {
			JSONObject jsonObjects = menuList.getJSONObject(i);
			Map<String, Object> map = new HashMap<>();

			// 将JSONObject中的键值对转换为Map中的键值对
			map.put("userNamerOrRoleId", roleIdOrUsername);
			map.put("menuId", jsonObjects.getString("menuId"));
			menuLists.add(map);
		}

		return pageService.addMultiPage(list, menuLists, roleIdOrUsername).toJSONString();
	}

	// 不用的接口
	@RequestMapping("/upMultiPage")
	public String upMultiPage(@RequestBody JSONObject jsonObject) {
		String roleId = jsonObject.getString("roleIdOrUsername");
		JSONArray jsonArr = jsonObject.getJSONArray("list");
		List<Map<String, Object>> lists = new ArrayList<>();
		for (int i = 0; i < jsonArr.size(); i++) {
			JSONObject jsonObjects = jsonArr.getJSONObject(i);
			Map<String, Object> map = new HashMap<>();

			// 将JSONObject中的键值对转换为Map中的键值对
			map.put("roleIdOrUsername", jsonObjects.getString("roleIdOrUsername"));
			map.put("pageId", jsonObjects.getString("pageId"));
			lists.add(map);
		}
		return pageService.upMultiPage(roleId, lists).toJSONString();
	}

	@RequestMapping("/addTbPage")
	public String addTbPage(@RequestBody JSONObject jsonObject) {
		return pageService.addTbPage(jsonObject).toJSONString();
	}

	@RequestMapping("/upTbPage")
	public String upTbPage(@RequestBody JSONObject jsonObject) {
		return pageService.upTbPage(jsonObject).toJSONString();
	}

	@RequestMapping("/delTbPage")
	public JSONObject delTbPage(@RequestBody JSONObject jsonObject) {
		Integer pageId = jsonObject.getInteger("pageId");
		return pageService.delTbPage(pageId);
	}
}
